package top.infopub.security.core;


import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.Ini.Section;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import top.infopub.security.service.SysPurviewService;
import top.infopub.security.vo.PurviewVO;


/**
 * 初始化加载所有权限， 用于通过当前页面url获得对应访问权限code
 * @author Awoke
 * @version 2018年9月17日
 * @see ChainDefinitionSectionMetaSource
 * @since
 */
public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section> {

    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;

    private String filterChainDefinitions;

    /**
     * 是否对所有的路径进行判断需要登录
     */
    private boolean alluserLogin = false;

    @Autowired
    private SysPurviewService sysPurviewService;

    /**
     * 默认premission字符串
     */
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    public Section getObject()
        throws BeansException {
        // 获取所有资源
        Ini ini = new Ini();
        // 加载默认的url
        ini.load(filterChainDefinitions);
        Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);

        Map<String, List<String>> purviews = new HashMap<String, List<String>>();

        List<PurviewVO> _PurviewVOList = new ArrayList<PurviewVO>();
        _PurviewVOList = sysPurviewService.queryAll();
        for (PurviewVO pur : _PurviewVOList) {
            String purviewurl = pur.getPurviewUrl();
            //  权限url使用逗号分隔 
            if (purviewurl.contains(";")) {
                String[] temp = pur.getPurviewUrl().split(";");
                for (int i = 0; i < temp.length; i++ ) {
                    if (StringUtils.isNotEmpty(temp[i])
                        && StringUtils.isNotEmpty(pur.getPurviewCode())) {
                        if (purviews.containsKey(temp[i])) {
                            purviews.get(temp[i]).add(pur.getPurviewCode());
                        }
                        else {
                            List<String> purCodes = new ArrayList<String>();
                            purCodes.add(pur.getPurviewCode());
                            purviews.put(temp[i], purCodes);
                        }
                    }
                }
            }
            else {
                if (purviews.containsKey(purviewurl)) {
                    purviews.get(purviewurl).add(pur.getPurviewCode());
                }
                else {
                    List<String> purCodes = new ArrayList<String>();
                    purCodes.add(pur.getPurviewCode());
                    purviews.put(purviewurl, purCodes);
                }
            }
        }
        for (String key : purviews.keySet()) {
            section.put(
                key,
                "user,concurrentlogin,"
                    + MessageFormat.format(PREMISSION_STRING,
                        StringUtils.join(purviews.get(key), ',')));
        }

        if (alluserLogin) {
            section.put("/**", "user,concurrentlogin");
        }
        return section;
    }

    /**
     * 默认的url过滤定义
     */
    public void setFilterChainDefinitions(String filterChainDefinitions) {
        this.filterChainDefinitions = filterChainDefinitions;
    }

    public Class<?> getObjectType() {
        return this.getClass();
    }

    public boolean isSingleton() {
        return false;
    }

    public boolean isAlluserLogin() {
        return alluserLogin;
    }

    public void setAlluserLogin(boolean alluserLogin) {
        this.alluserLogin = alluserLogin;
    }

}